package com.mmsj;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class PopupMenuActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup_menu);

        ImageView imageView = findViewById(R.id.imageView);
        imageView.setOnClickListener(view -> {
            // 创建 弹出菜单
            PopupMenu popupMenu = new PopupMenu(this, view);
            popupMenu.inflate(R.menu.my_menu);
            popupMenu.show();
            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    int id = item.getItemId();
                    if(id == R.id.menu_item_1)
                        Toast.makeText(PopupMenuActivity.this, "menu_item_1", Toast.LENGTH_SHORT).show();
                    else if(id == R.id.menu_item_2)
                        Toast.makeText(PopupMenuActivity.this, "menu_item_2", Toast.LENGTH_SHORT).show();
                    else if(id == R.id.menu_item_3)
                        Toast.makeText(PopupMenuActivity.this, "menu_item_3", Toast.LENGTH_SHORT).show();
                    else if (id == R.id.menu_item_4)
                        Toast.makeText(PopupMenuActivity.this, "menu_item_4", Toast.LENGTH_SHORT).show();
                    else if (id == R.id.menu_item_5)
                        Toast.makeText(PopupMenuActivity.this, "menu_item_5", Toast.LENGTH_SHORT).show();
                    return true;
                }
            });
        });

    }
}